﻿using DesktopShortcutMonitor.Model;
using DesktopShortcutMonitor.Monitor;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DesktopShortcutMonitor
{
    public partial class SettingsForm : UIForm
    {
        Config _config;

        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            _config = App.Ins.Config;

            nudScanIntervalSec.Value = _config.ScanIntervalSec;
            grid.DataSource = _config.Datas;

            // 启用编辑功能
            grid.ReadOnly = false;
            grid.AllowUserToAddRows = true;
            grid.AllowUserToDeleteRows = true;

            //控件最小尺寸
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            grid.AllowUserToResizeColumns = true;
        }



        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_config.Datas.Count() < 1)
            {
                UIMessageBox.ShowError("配置被删光了，无法保存");
                return;
            }
            _config.ScanIntervalSec = Convert.ToInt32(nudScanIntervalSec.Value);
            App.Ins.SaveDic();
        }

        private void 增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var varInfo = new SingleConfig();
            string varName = string.Empty;
            if (UIInputDialog.ShowInputStringDialog(ref varName, UIStyle.Blue, true, "请输入要检测程序的关键名称"))
            {
                if (_config.Datas.Where(s => s.CheckLinkNamePattern.Contains(varName)).Count() > 0)
                {
                    UIMessageBox.ShowError($"变量{varName}已存在，请重新输入");
                    return;
                }

                varInfo.CheckLinkNamePattern = varName;
            }

            var typeList = new List<bool> { false, true };
            int selIndex = 0;
            if (this.ShowSelectDialog(ref selIndex, typeList))
            {
                varInfo.CheckContainFile = typeList[selIndex];
            }

            _config.Datas.Add(varInfo);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (grid.CurrentRow?.DataBoundItem is SingleConfig)
            {
                SingleConfig varInfo = grid.CurrentRow?.DataBoundItem as SingleConfig;
                _config.Datas.Remove(varInfo);
            }
        }
    }
}
